Page({
  data: {
    current_page: 1,
    list: [],
  },

  onLoad(opts) {
    let that = this
    console.log(opts)
    that.setData({
      c_name:opts.c_name,
      g_name:opts.g_name
    })
    wx.showLoading({
      title: '加载中...',
    })
  },

  onShow() {
    let that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页
    console.log(currPage.data)
    if(currPage.data.status == 1){
      that.setData({
        current_page:1
      })
    }
    console.log(that.data.current_page)
    that.loadlist(that.data.current_page)
  },

  loadlist(current_page) {
    let that = this
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/houselist',
      data: {
        access_token: access_token,
        current_page: current_page,
        page_size: 15
      },
      success: (res) => {
        console.log(res)
        console.log(that.data.current_page)
        if (res.data.code == 1) {
          if (that.data.current_page > 1) {
            // 已经渲染到页面上的list
            let list = that.data.list
            that.setData({
              list: list.concat(res.data.list),
              current_page: current_page + 1
            })
          } else {
            that.setData({
              list: res.data.list,
              current_page: current_page + 1
            })
          }
          wx.hideLoading()
        }
      },   
    })
  },

  onReachBottom() {
    let that = this
    console.log('上拉加载更多')
    that.loadlist(that.data.current_page)
  },

  delete(e) {
    let that = this
    that.setData({
      current_page:1
    })
    let id = e.currentTarget.dataset.id
    let access_token = wx.getStorageSync('access_token')
    wx.showModal({
      content: '确定要删除该信息吗？',
      success: (res) => {
        if (res.confirm) {
          console.log('点击删除')
          wx.request({
            url: 'https://baokang.wshoto.com/wx/basicdel',
            data: {
              access_token: access_token,
              type: 1,
              id: id
            },
            success: (res) => {
              console.log(res)
              if (res.data.code == 1) {
                wx.showToast({
                  title: '删除成功',
                })
                that.loadlist(that.data.current_page)
              }
            }
          })
        }
      }
    })
  },

  add(){
    wx.navigateTo({
      url: '/pages/house-add/house-add',
    })
  },

  edit(e){
    console.log(e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/house/house?id=' + id,
    })
  }
})
